package com.share.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.domain.R;
import com.share.common.rabbit.constant.MqConst;
import com.share.common.rabbit.service.RabbitService;
import com.share.order.api.RemoteOrderInfoService;
import com.share.order.api.domain.OrderInfo;
import com.share.payment.domain.PaymentInfo;
import com.share.payment.mapper.PaymentInfoMapper;
import com.share.payment.service.PaymentService;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author YC
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentService {

    @Autowired
    private RemoteOrderInfoService remoteOrderInfoService;
    @Autowired
    private RabbitService rabbitService;

    @Override
    public PaymentInfo savePaymentInfo(String orderNo) {
        // 根据订单编号查询支付记录
        PaymentInfo paymentInfo = baseMapper.selectOne(
                new LambdaQueryWrapper<PaymentInfo>()
                        .eq(PaymentInfo::getOrderNo, orderNo)
        );
        // 如果支付记录不存在，进行添加
        if (paymentInfo == null) {
            // 根据订单编号查询订单数据（远程调用）
            OrderInfo orderInfo = remoteOrderInfoService.getByOrderNo(orderNo).getData();
            // 添加数据
            paymentInfo = new PaymentInfo();
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setContent("共享充电宝租借");
            paymentInfo.setAmount(orderInfo.getTotalAmount());
            paymentInfo.setOrderNo(orderNo);
            paymentInfo.setPaymentStatus(0);
            baseMapper.insert(paymentInfo);
        }
        return paymentInfo;
    }

    /**
     * 微信支付成功后的业务逻辑：
     * 更新支付记录状态为已经支付
     */
    @Override
    public void updatePaymentStatus(Transaction transaction) {
        String outTradeNo = transaction.getOutTradeNo();
        // 根据订单编号查询支付记录
        PaymentInfo paymentInfo = baseMapper.selectOne(
                new LambdaQueryWrapper<PaymentInfo>()
                        .eq(PaymentInfo::getOrderNo, outTradeNo)
        );
        // 判断支付记录状态是否已经修改
        Integer paymentStatus = paymentInfo.getPaymentStatus();
        if (paymentStatus == 1) {
            return;
        }
        //没有修改，再进行修改为已经支付
        paymentInfo.setPaymentStatus(1);
        paymentInfo.setOrderNo(transaction.getOutTradeNo());
        paymentInfo.setTransactionId(transaction.getTransactionId());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(com.alibaba.fastjson.JSON.toJSONString(transaction));
        baseMapper.updateById(paymentInfo);
        // 修改订单状态（RabbitMQ发送到队列，由订单模块接收处理）
        rabbitService.sendMessage(
                MqConst.EXCHANGE_PAYMENT_PAY,
                MqConst.ROUTING_PAYMENT_PAY,
                paymentInfo.getOrderNo()
        );
    }

}
